class Solution {
public:
    int maxProduct(vector<int>& nums) {
        vector<int> f(nums.size() + 1);
        auto g = f;
        int ret = INT_MIN;
        f[0] = g[0] = 1;
        for (int i = 1; i <= nums.size(); i++) {
            f[i] = max(nums[i - 1],
                       max(f[i - 1] * nums[i - 1], g[i - 1] * nums[i - 1]));
            g[i] = min(nums[i - 1],
                       min(f[i - 1] * nums[i - 1], g[i - 1] * nums[i - 1]));
            ret = max(ret, f[i]);
        }
        return ret;
    }
};